/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ListaProcView.java
 *
 * Created on 14/10/2009, 11:36:26
 */

package view.processo;

import Util.manipulaConexao;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author Administrador
 */
public class ListaProcView extends javax.swing.JDialog {

    /** Creates new form ListaProcView */
    public ListaProcView(java.awt.Frame parent, boolean modal, ModelagemView model) {
        super(parent, modal);
        initComponents();
        this.model = model;
        String sql = "Select idprocesso, nome from processo";
        manipulaConexao.preenchetable(jtbprocessos,sql);
        jtbprocessos.getSelectionModel().addListSelectionListener(selectionListener);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jtbprocessos = new javax.swing.JTable();
        jlpesquisar = new javax.swing.JLabel();
        jtpesquisar = new javax.swing.JTextField();
        jbtok = new javax.swing.JButton();
        jbtcancel = new javax.swing.JButton();

        setTitle("Processos");
        setResizable(false);

        jtbprocessos.getTableHeader().setReorderingAllowed(false);
        jtbprocessos.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtbprocessosMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jtbprocessos);

        jlpesquisar.setText("Pesquisar :");

        jbtok.setText("OK");
        jbtok.setPreferredSize(new java.awt.Dimension(73, 23));
        jbtok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtokActionPerformed(evt);
            }
        });

        jbtcancel.setText("Cancelar");
        jbtcancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtcancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jtpesquisar, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtok, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtcancel))
                    .addComponent(jlpesquisar))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 209, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jlpesquisar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtpesquisar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtcancel)
                    .addComponent(jbtok, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(16, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-416)/2, (screenSize.height-321)/2, 416, 321);
    }// </editor-fold>//GEN-END:initComponents

    private void jbtokActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtokActionPerformed
        if(id != 0)
        {
            model.setId(id);
            model.carregaArvore(model.getId());
            model.status(0);
            dispose();
        }
        return;
    }//GEN-LAST:event_jbtokActionPerformed

    private void jbtcancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtcancelActionPerformed
        this.dispose();
    }//GEN-LAST:event_jbtcancelActionPerformed

    private void jtbprocessosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtbprocessosMouseClicked
        if(evt.getClickCount() == 2)
        {
            jbtok.doClick();
        }
    }//GEN-LAST:event_jtbprocessosMouseClicked

    private ListSelectionListener selectionListener = new ListSelectionListener()
    {
        public void valueChanged(ListSelectionEvent e)
        {
            int linha = jtbprocessos.getSelectedRow();
            if(linha == -1) return;


            id =  Integer.valueOf(String.valueOf(jtbprocessos.getValueAt(linha, 0)));

        }
    };


    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                ListaProcView dialog = new ListaProcView(new javax.swing.JFrame(), true, new ModelagemView(new Integer(0)));
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtcancel;
    private javax.swing.JButton jbtok;
    private javax.swing.JLabel jlpesquisar;
    private javax.swing.JTable jtbprocessos;
    private javax.swing.JTextField jtpesquisar;
    // End of variables declaration//GEN-END:variables
    ModelagemView model;
    private int id;
}
